﻿using System.IO;
using System.Threading.Tasks;
using PinFun.Wpf;

namespace CodeLines
{
    public class CodeFile : ViewModelBase
    {
        public CodeFile(string file)
        {
            FullPath = file;
            Name = Path.GetFileName(file);
            State = RunState.Pending;
        }

        public string FullPath
        {
            get => GetValue(() => FullPath);
            set => SetValue(() => FullPath, value);
        }

        public string Name
        {
            get => GetValue(() => Name);
            set => SetValue(() => Name, value);
        }

        public RunState State
        {
            get => GetValue(() => State);
            set => SetValue(() => State, value);
        }

        public int Code
        {
            get => GetValue(() => Code);
            set => SetValue(() => Code, value);
        }

        public Task ReadLineCount()
        {
            return Task.Run(() =>
            {
                try
                {
                    State = RunState.Running;
                    using var sr = new StreamReader(FullPath);
                    while ((sr.ReadLine()) != null)
                    {
                        Code++;
                    }

                    sr.Close();
                    State = RunState.Complete;
                }
                catch
                {
                    State = RunState.Error;
                    throw;
                }
            });
        }
    }
}